package eu.whowie.jast.server.web;

import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.vaadin.ui.AbstractComponentContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

import eu.whowie.jast.remote.JastClientCallbackHandler;
import eu.whowie.jast.remote.ModuleGroup;
import eu.whowie.jast.remote.ModuleUIHandler;
import eu.whowie.jast.remote.YDialogType;
import eu.whowie.jast.remote.YJAlignmentInterface;
import eu.whowie.jast.remote.YJBusyIndicatorInterface;
import eu.whowie.jast.remote.YJButtonBoxInterface;
import eu.whowie.jast.remote.YJCheckBoxFrameInterface;
import eu.whowie.jast.remote.YJCheckBoxInterface;
import eu.whowie.jast.remote.YJComboBoxInterface;
import eu.whowie.jast.remote.JDialogInterface;
import eu.whowie.jast.remote.YJEmptyInterface;
import eu.whowie.jast.remote.YJFrameInterface;
import eu.whowie.jast.remote.YJImageInterface;
import eu.whowie.jast.remote.YJInputFieldInterface;
import eu.whowie.jast.remote.YJIntFieldInterface;
import eu.whowie.jast.remote.YJLabelInterface;
import eu.whowie.jast.remote.YJLayoutBoxInterface;
import eu.whowie.jast.remote.YJLogViewInterface;
import eu.whowie.jast.remote.YJMenuButtonInterface;
import eu.whowie.jast.remote.YJMultiLineEditInterface;
import eu.whowie.jast.remote.YJMultiSelectionBoxInterface;
import eu.whowie.jast.remote.YJPackageSelectorInterface;
import eu.whowie.jast.remote.YJProgressBarInterface;
import eu.whowie.jast.remote.YJPushButtonInterface;
import eu.whowie.jast.remote.YJRadioButtonGroupInterface;
import eu.whowie.jast.remote.YJRadioButtonInterface;
import eu.whowie.jast.remote.YJReplacePointInterface;
import eu.whowie.jast.remote.YJRichTextInterface;
import eu.whowie.jast.remote.YJSelectionBoxInterface;
import eu.whowie.jast.remote.YJSpacingInterface;
import eu.whowie.jast.remote.YJSquashInterface;
import eu.whowie.jast.remote.YJTableInterface;
import eu.whowie.jast.remote.YJTreeInterface;
import eu.whowie.jast.remote.YJWidgetInterface;
import eu.whowie.jast.remote.YTableHeader;
import eu.whowie.jast.remote.YastModule;
import eu.whowie.jast.remote.YastServer;

public class YastServerUIComponent {

	Logger logger = Logger.getLogger("SERVER_UI_COMP");
	private final YastServer server;
	private Button selectButton;
	private TabSheet tabSheet;
	private final JastClientCallbackHandler callBackHandler;

	public YastServerUIComponent(YastServer server,
			JastClientCallbackHandler callBackHandler) {
		this.server = server;
		this.callBackHandler = callBackHandler;

	}

	public Button getSelectButton() {
		if (selectButton == null) {
			selectButton = new Button(server.getName());
		}
		return selectButton;
	}

	private void startModule(YastModule module) {
		try {
			callBackHandler.startJastModule(module);
		} catch (RemoteException e) {
			logger.log(Level.SEVERE, "connection disrupted, can startModule", e);
		}
	}

	/**
	 * fill main Panel with component from selected server
	 */
	public AbstractComponentContainer getMainUI() {
		if (tabSheet == null) {
			tabSheet = new TabSheet();

			// panel.removeAllComponents(); // clear everything
			VerticalLayout groupLayout = new VerticalLayout();
			List<ModuleGroup> groupList = server.getModuleGroups();
			for (ModuleGroup moduleGroup : groupList) {
				HorizontalLayout moduleLayout = new HorizontalLayout();
				for (YastModule m : moduleGroup.getModules()) {
					Button module = new Button(m.getName());
					module.setData(m);
					module.addListener(new ClickListener() {

						@Override
						public void buttonClick(ClickEvent event) {
							Button b = (Button) event.getSource();
							YastModule m = (YastModule) b.getData();
							startModule(m);

						}
					});
					moduleLayout.addComponent(module);
				}
				groupLayout.addComponent(moduleLayout);
			}
			tabSheet.addTab(groupLayout, "Modules", null);
		}
		return tabSheet;
	}
}

class YastUIHandler implements ModuleUIHandler {

	private final YastServerUIComponent yastServerUIComponent;

	public YastUIHandler(YastServerUIComponent yastServerUIComponent) {
		this.yastServerUIComponent = yastServerUIComponent;
	}

	@Override
	public JDialogInterface createDialog(YDialogType dialogType, int colorMode) {
		// TODO Auto-generated method stub
		System.out.println("here");
		System.out.println("here");
		return null;
	}

	@Override
	public YJLabelInterface createLabel(YJWidgetInterface parent, String text,
			boolean isHeading, boolean isOutputField) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJTableInterface createTable(YJWidgetInterface parent,
			YTableHeader header_disown, boolean multiSelection) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJSpacingInterface createSpacing(YJWidgetInterface parent, int dim,
			boolean stretchable, int size) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJSquashInterface createSquash(YJWidgetInterface parent,
			boolean horSquash, boolean vertSquash) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJCheckBoxFrameInterface ceateCheckBoxFrame(
			YJWidgetInterface parent, String label, boolean checked) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJLayoutBoxInterface createLayoutBox(YJWidgetInterface widget,
			int dim) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJButtonBoxInterface createButtonBox(YJWidgetInterface widget) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJPushButtonInterface createPushButton(YJWidgetInterface widget,
			String string) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJInputFieldInterface createInputField(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJCheckBoxInterface createCheckBox(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJRadioButtonInterface createRadioButton(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJComboBoxInterface createComboBox(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJSelectionBoxInterface createSelectionBox(YJWidgetInterface widget,
			String string) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJTreeInterface createTree(YJWidgetInterface widget, String caption) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJProgressBarInterface createProgressBar(YJWidgetInterface widget,
			String string, int value) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJRichTextInterface createRichText(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJBusyIndicatorInterface createBusyIndicator(YJWidgetInterface w,
			String label, int timeout) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJIntFieldInterface createIntField(YJWidgetInterface w,
			String label, int min, int max, int init) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJMenuButtonInterface createMenuButton(YJWidgetInterface widget,
			String label) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJMultiLineEditInterface createMultiLineEdit(
			YJWidgetInterface widget, String label) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJImageInterface createImage(YJWidgetInterface widget,
			String string, boolean enabled) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJLogViewInterface createLogView(YJWidgetInterface widget,
			String string, int i, int j) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJMultiSelectionBoxInterface createMultiSelectionBox(
			YJWidgetInterface widget, String label) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJPackageSelectorInterface createPackageSelector(
			YJWidgetInterface widget, int flags) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJWidgetInterface createPkgSpecial(YJWidgetInterface w, String label) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJEmptyInterface createEmpty(YJWidgetInterface w) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJAlignmentInterface createAlignment(YJWidgetInterface widget,
			int horType, int verType) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJFrameInterface createFrame(YJWidgetInterface widget, String caption) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJRadioButtonGroupInterface createRadioButtonGroup(
			YJWidgetInterface w) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

	@Override
	public YJReplacePointInterface createReplacePoint(YJWidgetInterface w) {
		// TODO Auto-generated method stub
		System.out.println("here");
		return null;
	}

}
